package com.threeti.jiayusms.domain.service.communicationrule;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.threeti.jiayusms.domain.model.CommunicationRule;
import com.threeti.jiayusms.framework.util.DynaSearchCriteria;
import com.threeti.jiayusms.framework.util.PagedListHandler;
import com.threeti.jiayusms.infrastructure.communicationrule.CommunicationRuleDao;
import com.threeti.jiayusms.interfaces.RuleTemplateDynaCriteriaDto;

@Service
@Transactional(readOnly=true)
public class CommunicationRuleQueryServiceImpl implements CommunicationRuleQueryService {
	@Autowired
	private CommunicationRuleDao dao;

	@Override
	public CommunicationRule findCommunicationRuleBy(Long ruleId) {
		return dao.loadBy(ruleId, true);
	}
	
	@Override
	public CommunicationRule findCommunicationRuleBy(String ruleNo) {
		return dao.loadBy(ruleNo);
	}
	
	@Override
	public PagedListHandler<CommunicationRule> findAllRuleTemplatesByPage(int currentPageIndex, int startingIndex) {
    return dao.findAllRuleTemplatesByPage(currentPageIndex, startingIndex);
	}
	
	@Override
	public PagedListHandler<CommunicationRule> findRuleTemplatesByPage(DynaSearchCriteria dynaSearchCriteria, int currentPageIndex, int startingIndex) {
		RuleTemplateDynaCriteriaDto dynaCriteria = (RuleTemplateDynaCriteriaDto)dynaSearchCriteria;
		String templateKeywords = dynaCriteria.getTemplateKeywords();
		if (!StringUtils.hasText(templateKeywords)) {//无条件查询
			return dao.findAllRuleTemplatesByPage(currentPageIndex, startingIndex);
		}
    return dao.findRuleTemplatesByPage(dynaSearchCriteria, currentPageIndex, startingIndex);
	}
}
